package interfaceConfig

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	"gitee.com/liuxuezhan/ar-platform/admin-api/pkg/code"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/interfaceconfig"
	"github.com/zeromicro/go-zero/core/logc"
	"github.com/zeromicro/go-zero/core/logx"
)

type InsertInterfaceConfigLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 新增或修改
func NewInsertInterfaceConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InsertInterfaceConfigLogic {
	return &InsertInterfaceConfigLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *InsertInterfaceConfigLogic) InsertInterfaceConfig(req *types.AddInterfaceConfigReq) (resp *types.Empty, err error) {

	phone := make([]*interfaceconfig.PhoneConfig, 0)
	for _, v := range req.PhoneConfig {
		phone = append(phone, &interfaceconfig.PhoneConfig{
			AreaCode: v.AreaCode,
			PhoneNum: v.PhoneNum,
		})
	}
	u, err := l.svcCtx.SettingService.InterfaceConfig.UpdateInterfaceConfig(l.ctx, &interfaceconfig.AddInterfaceConfigReq{
		MerchantId:      1,
		Logo:            req.Logo,
		Logo1:           req.Logo1,
		Icon:            req.Icon,
		SiteName:        req.SiteName,
		LanguageList:    req.LanguageList,
		DefaultLanguage: req.DefaultLanguage,
		Currency:        req.Currency,
		NameShowType:    req.NameShowType,
		PhoneConfig:     phone,
	})

	if err != nil {
		logc.Error(l.ctx, "InsertInterfaceConfig error:", err)
		return nil, err
	}

	if u.Code != int64(code.SettingRpcSuccessful.Code()) {
		return nil, code.BusinessFailure
	}
	resp = &types.Empty{}
	return
}
